<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%--<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="GBK"%>--%>
<!DOCTYPE html>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="from"%>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/style/main.css">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/style/icon.css">
</head>

<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/js/ajaxfileupload.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/js/caseData-design.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/js/registerNewBotton.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/js/ajaxupload.3.6.js"></script>
<script type="text/javascript">
    var ue = UE.getEditor('editor');
    var remarkue = UE.getEditor('remark_editor');
    var start = new Date();
    var caseFieldData = <%=request.getAttribute("caseFieldData") %>;
    var fieldCatalogName="<spring:message code="field_catalog_name"/>";
    var fieldFloatName="<spring:message code="field_data_float_name"/>";
    var fieldTextName="<spring:message code="field_data_text_name"/>";
    var fieldPictureName="<spring:message code="field_data_picture_name"/>";
    var fieldAttachName="<spring:message code="field_data_attach_name"/>";
    var fieldMatrixName="<spring:message code="field_data_matrix_name"/>";
    var fieldMultiName="<spring:message code="field_data_multi_name"/>";
    var field3DName = "<spring:message code="field_data_3D_name"/>";
    var fieldVideoName = "<spring:message code="field_data_video_name"/>";
    var fieldTimeName = "<spring:message code="field_data_time_name"/>";
    var fieldLinkName = "<spring:message code="field_data_link_name"/>";
    var field3DMatrixName = "<spring:message code="field_data_3matrix_name"/>";
    var caseId = <%=request.getAttribute("caseId")%>;
    var editId;
    var case_data_table = document.getElementById("case_data");
    //case_data_table.setAttribute("style","height:"+(document.body.scrollHeight-118)+"px");
    var p = {};
    var progressBar = null;
    var intervalId = null;
    p['caseId'] = caseId;
    $(function(){
        $('#case_data').treegrid({
            data:caseFieldData,
            onDblClickCell:function(field,row){
                editId = row.id;
                if(field=="remark"){
                    editId = row.id;
                    $('#remarkWindow').window('open');
                    remarkue.setContent(row.remark_hidden);
                }
                if(editId==undefined){
                    editId = row.id;
                }
                if(field!="remark"){
                    if(row.attributes==fieldFloatName){
                        $(this).treegrid('beginEdit',editId);
                    }else if(row.attributes==fieldTextName){
                        var last = $(this).treegrid('find',editId);
                        TextPre(caseId, last.id);
                        $('#fieldTextWindow').window('open');
                    }else if(row.attributes==fieldPictureName){
                        var last = $(this).treegrid('find',editId);
                        picPre(caseId, last.id);
                        $('#fieldPicWindow').window('open');
                    }else if(row.attributes==fieldAttachName){
                        var last = $(this).treegrid('find',editId);
                        attachPre(caseId, last.id);
                        $('#fieldAttachWindow').window('open');
                    }else if(row.attributes==fieldMatrixName){
                        var last = $(this).treegrid('find',editId);
                        MatrixPre(caseId, last.id);
                        $('#fieldMatrixWindow').window('open');
                    }else if(row.attributes==fieldMultiName){
                        $('#fieldMultiWindow').window('open');
                        ue.setContent(row.value_hidden);
                    }else if(row.attributes==field3DName){
                        var last = $(this).treegrid('find',editId);
                        threeDPre(caseId, last.id);
                        $('#field3DWindow').window('open');
                    }else if(row.attributes==fieldVideoName){
                        var last = $(this).treegrid('find',editId);
                        videoPre(caseId, last.id);
                        $('#fieldVideoWindow').window('open');
                    }else if(row.attributes==fieldTimeName){
                        $(this).treegrid('beginEdit',editId);
                    }else if(row.attributes==fieldLinkName){
                        var last = $(this).treegrid('find',editId);
                        LinkPre(caseId, last.id);
                        $('#caseLinkWindow').window('open');
                     }else if(row.attributes==field3DMatrixName){
                        var last = $(this).treegrid('find',editId);
                        threeDMatrixPre(caseId, last.id);
                        $('#field3DMatrixWindow').window('open');
                    }
                }
            }
            ,onBeforeEdit:function(row){
                var valueCol = $('#case_data').treegrid('getColumnOption', 'data_value');
                if(row.attributes==fieldTimeName){
                    valueCol.editor = 'datebox';
                }else{
                    valueCol.editor = 'text';
                }
            }
        });
    });
    $('#case_data').treegrid('expandAll',0);
    var end = new Date();
//    alert(end.getTime()-start.getTime());
    var tableUrl={
        caseUpdate:'<%=request.getContextPath() %>/table/update',
        caseDelete:'<%=request.getContextPath() %>/table/delete',
        caseGetValue:'<%=request.getContextPath() %>/table/getValue',
        caseUpdateRemark:'<%=request.getContextPath() %>/table/updateRemark',
        caseGetUnit:'<%=request.getContextPath() %>/table/getFloatUnit',
        tMatrixXUpdate:'<%=request.getContextPath() %>/table/updateMatrixX',
        tMatrixYUpdate:'<%=request.getContextPath() %>/table/updateMatrixY',
        tMatrixZUpdate:'<%=request.getContextPath() %>/table/updateMatrixZ'
    };
    var uploadUrl={
        uploadMatrix:'<%=request.getContextPath() %>/upload/uploadMatrix',
        uploadThreeMatrix:'<%=request.getContextPath() %>/upload/uploadThreeMatrix',
        readMatrixJson:'<%=request.getContextPath() %>/upload/readMatrixJson',
        readThreeMatrixJson:'<%=request.getContextPath() %>/upload/readThreeMatrixJson',
        readyMatrixExcel:'<%=request.getContextPath() %>/upload/readyMatrixExcel',
        readyThreeMatrixExcel:'<%=request.getContextPath() %>/upload/readyThreeMatrixExcel',
        createMatrixJson:'<%=request.getContextPath() %>/upload/createMatrixJson',
        maIndex:'<%=request.getContextPath() %>/upload/maindex',
        uploadCase:'<%=request.getContextPath() %>/upload/uploadCase',
        uploadCases:'<%=request.getContextPath() %>/upload/uploadCases',
        readCase:'<%=request.getContextPath() %>/upload/readCase',
        readCases:'<%=request.getContextPath() %>/upload/readCases',
        FileUploadA:'<%=request.getContextPath() %>/upload/fileUpload', 
        updateMatrixExcel:'<%=request.getContextPath() %>/upload/updateMatrixExcel',
        downloadCase:'<%=request.getContextPath() %>/upload/downloadCase',
        downloadCases:'<%=request.getContextPath() %>/upload/downloadCases',
        startCaseDownLoad:'<%=request.getContextPath() %>/upload/startCaseDownLoad',
        clearZipAndDir:'<%=request.getContextPath() %>/upload/clearZipAndDir'
    };
    var unit = {};
    $.ajax({
        url:tableUrl.caseGetUnit,
        type:'post',
        data:p,
        success:function(result){
            if(result.successful){
                var mes = result.message;
                var mes_arr1 = mes.split(";");
                for(var i=0;i<mes_arr1.length;i++){
                    unit[mes_arr1[i].split(",")[0]] = mes_arr1[i].split(",")[1];
                }
            }
        }
    });

    function saveData(){
        if(editId!=undefined){
            var root = $('#case_data').treegrid('getRoot');
            var data_arr = [];
            var children = $('#case_data').treegrid('getChildren',root.id);
            for(var i=1;i<children.length;i++){
                data_arr.push(children[i]);
            }
            var floats = [];
            var times = [];
            for(var i=0;i<data_arr.length;i++){
                $('#case_data').treegrid('endEdit',data_arr[i].id);
                if(data_arr[i].attributes==fieldFloatName){
                
                    floats.push(data_arr[i]);
                }else if(data_arr[i].attributes==fieldTimeName){
                    times.push(data_arr[i]);
                }
            }
            params = {};
            for(var i=0;i<floats.length;i++) {
                var last = floats[i];
                var re = /^[0-9]+.?[0-9]*$/;
                params['caseId'] = caseId;
                params['value'] = last.data_value.split(" ")[0];
                //判断输入的是不是浮点数。
                 if (!re.test(last.data_value.split(" ")[0])){
                	 $.messager.alert('消息',"输入的不是浮点数，请重新输入！");                	
                 return false;
                } 
                params['fieldId'] = last.id;
                $.ajax({
                    url: tableUrl.caseUpdate,
                    type: 'post',
                    data: params,
                    success: function (result) {
                        if (result.successful) {

                        }
                    }
                });
            }
            for(var i=0;i<floats.length;i++){
                var last = floats[i];
                if(last.data_value!=""&&last.data_value!=" "&&last.data_value!=null){
                    $('#case_data').treegrid('update',{
                        id:last.id,
                        row:{
                            data_value:last.data_value.split(" ")[0] + "     " + unit[last.id]
                        }
                    });
                }

            }
            for(var i=0;i<times.length;i++) {
                var last = times[i];
                params['caseId'] = caseId;
                params['value'] = last.data_value;
                params['fieldId'] = last.id;
                $.ajax({
                    url: tableUrl.caseUpdate,
                    type: 'post',
                    data: params,
                    success: function (result) {
                        if (result.successful) {

                        }
                    }
                });
            }
            saveSuccess();
        }
    }

    function saveMulti(){
        $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
        params = {};
        params['caseId'] = caseId;
        params['value'] = ue.getContent();
        params['fieldId'] = last.id;
        $.ajax({
            url:tableUrl.caseUpdate,
            type:'post',
            data:params,
            success:function(result){
                if(result.successful){
                    var show = ue.getContentTxt();
                    if(show.length>10){
                        show = show.substring(0,10)+"...";
                    }
                    $('#case_data').treegrid('update',{
                        id:last.id,
                        row:{
                            data_value:show,
                            value_hidden:ue.getContent()
                        }
                    });
                    //saveSuccess();
                    $('#fieldMultiWindow').window('close');
                }
            }
        });
    }
    function saveRemark(){
        $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
        params = {};
        params['caseId'] = caseId;
        params['remark'] = remarkue.getContent();
        params['fieldId'] = last.id;
        $.ajax({
            url:tableUrl.caseUpdateRemark,
            type:'post',
            data:params,
            success:function(result){
                if(result.successful){
                    var show = remarkue.getContentTxt();
                    if(show.length > 10){
                        show = show.substring(0,10)+"...";
                    }
                    $('#case_data').treegrid('update',{
                        id:last.id,
                        row:{
                            remark:show,
                            remark_hidden:remarkue.getContent()
                        }
                    });
                    //saveSuccess();
                    $('#remarkWindow').window('close');
                }
            }
        });
        params = {};
        if(last.attributes==fieldFloatName){
            params['value'] = last.data_value.split(" ")[0];
            $.ajax({
                url:tableUrl.caseUpdate,
                type:'post',
                data:params,
                success:function(result){
                    if(result.successful){
                    }
                }
            });
        }
    }
    function saveText(){
        $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
        params = {};
        params['caseId'] = caseId;
        params['value'] = document.getElementById('multiTextBox').value;
        params['fieldId'] = last.id;
        $.ajax({
            url:tableUrl.caseUpdate,
            type:'post',
            data:params,
            success:function(result){
                if(result.successful){
                    var show = "";
                    if(params['value'].length > 10){
                        show = params['value'].substring(0,10) + "...";
                    }else{
                        show = params['value'];
                    }
                    $('#case_data').treegrid('update',{
                        id:last.id,
                        row:{
                            data_value:show
                        }
                    });
                    //saveSuccess();
                    $('#fieldTextWindow').window('close');
                }
            }
        });
    }
    function saveSuccess(){
        $.messager.alert('消息','保存成功!');
    }
    function importData(){
        $('#caseData_import').click();
    }
    function showProgress(isShow, title, msg, value){
        if(!isShow){
            $.messager.progress('close');
            return;
        }
        $.messager.progress({
            title:title,
            msg:msg,
            interval:0
        });
        progressBar = $.messager.progress('bar');
        intervalId = setInterval(changeProgress(value,progressBar,5),500);
    }
    function changeProgress(value,progressBar,step){
        return function() {
            if (value[0] < 100) {
                value[0] += Math.floor(Math.random()*step);
                progressBar.progressbar('setValue', value[0]);
            }else{
                value[0] = 0;
                progressBar.progressbar('setValue', value[0]);
            }
        }
    }
    function exportData(){
        var progressValue = [];
        progressValue[0] = 0;
        $.ajax({
            url:uploadUrl.downloadCase,
            data:'caseId='+caseId,            
            type:'get',
            onSubmit:showProgress(true, "提示", "服务器处理中...", progressValue),
            success:function(result){
                if(result.successful){
                    if(progressBar!=null){
                        clearInterval(intervalId);
                        progressValue[0] = progressBar.progressbar('getValue');
                        intervalId = setInterval(changeProgress(progressValue,progressBar,5),500);
                    }
                    setTimeout(function(){
                        //clearInterval(intervalId);
                        progressBar.progressbar('setValue', 100);
                    },5000);
                    setTimeout(function(){
                        $.messager.progress('close');
                        clearInterval(intervalId);
                        window.open(uploadUrl.startCaseDownLoad+"?caseId="+caseId);
                    },10000);
                }
            }
        });
    }
    function caseDataChange(){
        var ids = [];
        ids[0] = 'caseData_import';
        var filename = $('#caseData_import').val();
        filename = filename.substring(filename.lastIndexOf("\\")+1);
        $.ajaxFileUpload({
            url:uploadUrl.uploadCase,
            secureuri:false,
            fileElementId:ids,
            dataType:'json',
            success:function(data,status){
                if(data){
                    //if file uploaded is json
                    $.ajax({
                        url:uploadUrl.readCase,
                        data:'filename='+filename+'&caseId='+caseId,
                        type:'get',
                        success:function(result){                        	
                            if(result.successful){
                                $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+caseId);
                            }                          
                        }
                    });
                }
                else{
                    $.messager.alert("提示",data.result,"error");
                }
            },
            error:function(data,status,e){
            }
        });
    }
</script>
<div class="easyui-panel">
    <table id="case_data" class="easyui-treegrid" data-options="idField:'id',treeField:'text'" animate="false" rownumbers="true">
        <div id="toolbar">
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="saveData()"><spring:message code="case_design_save"/></a>
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-undo" plain="true" onclick="importData()">导入</a>
            <input id="caseData_import" type="file" name="file" style="display:none" onchange="caseDataChange()"/>
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" plain="true" onclick="exportData()">导出BB</a>
        </div>
        <thead>
        <tr>
            <th data-options="field:'text'" width="350" align="left"><spring:message code="field_text"/></th>
            <th data-options="field:'attributes'" width="200" align="center"><spring:message code="field_attr"/></th>
            <th id="value_editor" data-options="field:'data_value'" width="200" align="center" editor="text"><spring:message code="field_value"/></th>
            <th data-options="field:'value_hidden'" hidden="true"></th>
            <th data-options="field:'remark'" width="200" align="center"><spring:message code="field_remark"/></th>
            <th data-options="field:'remark_hidden'" hidden="true"></th>
        </tr>
        </thead>
    </table>
</div>
<div class="easyui-window" id="fieldMultiWindow" title="富文本编辑" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:1000px;height:500px;text-align: right">
    <script id="editor" type="text/plain" style="width:982px;height:300px;"></script>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveMulti()"><spring:message code="case_design_save"/> </a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#fieldMultiWindow').window('close')"><spring:message code="case_design_cancel"/> </a>
</div>
<div class="easyui-window" id="remarkWindow" title="备注编辑" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:1000px;height:500px;text-align: right">
    <script id="remark_editor" type="text/plain" style="width:982px;height:300px;"></script>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveRemark()"><spring:message code="case_design_save"/> </a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#remarkWindow').window('close')"><spring:message code="case_design_cancel"/> </a>
</div>
<div class="easyui-window" id="fieldAttachWindow" title="附件上传" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:400px;height: 250px;">
    <a href="javascript:void(0)" id="AttachAdd" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加 </a>
    <a href="javascript:void(0)" id="AttachSave" class="easyui-linkbutton" iconCls="icon-save" plain="true">保存 </a>
    <table name="AttachTable" width="350"></table>
</div>
<div class="easyui-window" id="fieldPicWindow" title="图片上传88" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:400px;height: 250px;">
    <a href="javascript:void(0)" id="PicAdd" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
    <a href="javascript:void(0)" id="PicSave" class="easyui-linkbutton" iconCls="icon-save" plain="true">保存 </a>
    <table name="PicTable" width="350"></table>
</div>
<div class="easyui-window" id="field3DWindow" title="三维模型上传" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:400px;height: 250px;">
    <a href="javascript:void(0)" id="ThreeDAdd" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加 </a>
    <a href="javascript:void(0)" id="ThreeDSave" class="easyui-linkbutton" iconCls="icon-save" plain="true">保存 </a>
    <table name="ThreeDTable" width="350"></table>
</div>
<div class="easyui-window" id="fieldVideoWindow" title="视频上传" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:400px;height: 250px;">
    <a href="javascript:void(0)" id="VideoAdd" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加 </a>
    <a href="javascript:void(0)" id="VideoSave" class="easyui-linkbutton" iconCls="icon-save" plain="true">保存 </a>
    <table name="VideoTable" width="350"></table>
</div>
<div class="easyui-window" id="fieldTextWindow" title="文本编辑" minimizable="false" maximizable="false" closed="true" collapsible="false" style="position:relative;width:600px;height: 350px;">
    <textarea id="multiTextBox" rows="18" cols="80" value=""></textarea>
    <div style="position: absolute;bottom:0px;right:0px">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveText()"><spring:message code="case_design_save"/> </a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#fieldTextWindow').window('close')"><spring:message code="case_design_cancel"/> </a>
    </div>
</div>
<div class="easyui-window" id="fieldMatrixWindow" title="二维表编辑" minimizable="false" maximizable="true" closed="true" collapsible="false" style="width:700px;height:350px;">
    <table id="matrix_data" style='height:300px;color:red;' class="easyui-datagrid"
           data-options="singleSelect:true,collapsible:true,toolbar:matrix_toolbar,onClickCell: onClickCell,resizable:true" animate="false" rownumbers="true" noheader="true" resizable="true">
        <thead >
        <tr>
        </tr>
        </thead>
    </table>   
  <input id="matrix_import" type="file"  name="file" style='display:none;' onchange="matrixChange()"/> 
</div>
<div class="easyui-window" id="field3DMatrixWindow" title="三维表编辑" minimizable="false" maximizable="true" closed="true" collapsible="false" style="width:700px;height: 350px;">
    <table id="threeDmatrix_data" class="easyui-datagrid"
           data-options="singleSelect:true,collapsible:true,toolbar:threeDmatrix_toolbar,onClickCell: onClickCellZ" animate="false" noheader="true">
        <thead><tr></tr></thead>
    </table>
    <input id="threeDmatrix_import" type="file" name="file" style="display:none" onchange="threeDmatrixChange()"/>
</div>
<div class="easyui-window" id="X_3DMatrixWindow" title="X编辑" minimizable="false" maximizable="true" closed="true" collapsible="false" style="width:600px;height: 350px;">
    <table id="threeDmatrix_data_X" class="easyui-datagrid"
           data-options="singleSelect:true,collapsible:true,toolbar:threeDmatrix_X_toolbar,onClickCell: onClickCellX" animate="false" noheader="true">
        <thead><tr></tr></thead>
    </table>
    <input id="threeDmatrix_import_X" type="file" name="file" style="display:none" onchange="threeDmatrixChangeX()"/>
</div>
<div class="easyui-window" id="Y_3DMatrixWindow" title="Y编辑" minimizable="false" maximizable="true" closed="true" collapsible="false" style="width:600px;height: 350px;">
    <table id="threeDmatrix_data_Y" class="easyui-datagrid"
           data-options="singleSelect:true,collapsible:true,toolbar:threeDmatrix_Y_toolbar,onClickCell: onClickCellY" animate="false" noheader="true">
        <thead><tr></tr></thead>
    </table>
    <input id="threeDmatrix_import_Y" type="file" name="file" style="display:none" onchange="threeDmatrixChangeY()"/>
</div>
<div class="easyui-window" id="caseLinkWindow" title="实例链接" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:500px;height: 600px;">
    <div style="position: fixed; margin-left:100px;background: white;height: 22px;width: 200px;z-index:10;">
        <a id="caseLink_ok" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="saveCaseLink()">确定</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="cancelCaseLink()">取消</a>
    </div>
    <div style="position: relative;top:25px;z-index: 9"><ul id="case_tree" class="easyui-tree"></ul></div>
</div>
<div class="easyui-window" id="imageLinkWindow" title="图片上传" minimizable="false" maximizable="false" closed="true" collapsible="false" style="width:400px;height: 250px;">
    <a href="javascript:void(0)" id="imageLinkAdd" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
    <a href="javascript:void(0)" id="imageLinkSave" class="easyui-linkbutton" iconCls="icon-save" plain="true">保存 </a>
    <table id="imageTable" width="350"></table>
</div>
<script type="text/javascript">
var matrix_toolbar = [{
    text:"增加一行",
    iconCls:'icon-add',
    handler:function(){
        //var columns = $('#matrix_data').datagrid('getColumnFields');
         $('#matrix_data').datagrid('appendRow',{
        });
    } 
},{
    text:"删除一行",
    iconCls:'icon-remove',
    handler:function(){
        var row = $('#matrix_data').datagrid('getSelected');
        var rowIndex = $('#matrix_data').datagrid('getRowIndex', row);
        $('#matrix_data').datagrid('deleteRow',rowIndex);       
    }
},{
    text:"导入",
    id:'excelId',
    iconCls:'icon-undo',
    handler:function(){
        $('#matrix_import').click();       
    }
},{
    text:"导出",
    iconCls:'icon-redo',
    handler:function(){
        //construct data json
         $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
     $('#matrix_data').datagrid('endEdit', editIndex);
        var columns = $('#matrix_data').datagrid('getColumnFields');         
        var matrix_data = $('#matrix_data').datagrid('getRows');    
        var rowLength = matrix_data.length;      
        var colLength = columns.length;     
        //join data
        var matrixData = [];
        for(var i=0;i<rowLength;i++){
            matrixData[i] = {};
            for(var j=0;j<colLength;j++){
                matrixData[i][columns[j]] = matrix_data[i][columns[j]];
            }
        }
//        //ajax
        params = {};
        params['caseId'] = caseId;     
        params['matrix'] = JSON.stringify(matrixData);
        $('#fieldMatrixWindow').window('close');
       openPostWindow(uploadUrl.maIndex, JSON.stringify(matrixData), "");
    }
},{
    text:"保存",
    iconCls:'icon-save',
    handler:function(){
        $('#matrix_data').datagrid('endEdit', editIndex);
        var columns = $('#matrix_data').datagrid('getColumnFields');     
        var matrix_data = $('#matrix_data').datagrid('getRows');
     
        var rowLength = matrix_data.length;
        var colLength = columns.length;
        //join data
        var matrixData = "";
        for(var i=0;i<rowLength;i++){
            for(var j=0;j<colLength;j++){
                if(j!=colLength-1)
                    matrixData += matrix_data[i][columns[j]] + ";";
                else           
                matrixData+= matrix_data[i][columns[j]];
            }
            if(i!=rowLength-1)
                matrixData += "#";
        }
        $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
        params = {};
        params['caseId'] = caseId;
        params['value'] = matrixData;
        params['fieldId'] = last.id;       
        $.ajax({
            url:tableUrl.caseUpdate,
            type:'post',
            data:params,
            success:function(result){
                if(result.successful){
                    var show = "";
                    if(params['value'].length > 10){
                        show = params['value'].substring(0,10) + "...";
                    }else{
                        show = params['value'];
                    }
                    $('#case_data').treegrid('update',{
                        id:last.id,
                        row:{
                            data_value:show
                        }
                    });
                    $('#fieldMatrixWindow').window('close');
                }
            }
        });
    }
}];

var threeDmatrix_toolbar = [{
    text:"X",
    iconCls:'icon-add',
    handler:function(){
        // open X window
        var last = $("#case_data").treegrid('find',editId);
        threeDMatrixPre_X(caseId, last.id);
        $('#X_3DMatrixWindow').window('open');
    }
},{
    text:"Y",
    iconCls:'icon-remove',
    handler:function(){
        // open Y window
        var last = $("#case_data").treegrid('find',editId);
        threeDMatrixPre_Y(caseId, last.id);
        $('#Y_3DMatrixWindow').window('open');
    }
},{
    text:"导入",
    iconCls:'icon-undo',
    handler:function(){
        //select file
        $('#threeDmatrix_import').click();
    }
},{
   /* 三维表exec导出*/
    text:"导出A",
    iconCls:'icon-redo',
    handler:function(){
    	  $('#case_data').treegrid('endEdit',editId);
          var last = $("#case_data").treegrid('find',editId);
       $('#threeDmatrix_data').datagrid('endEdit', editIndex);
          var columns = $('#threeDmatrix_data').datagrid('getColumnFields');         
          var matrix_data = $('#threeDmatrix_data').datagrid('getRows');    
          var rowLength = matrix_data.length;      
          var colLength = columns.length; 
          //join data
          var matrixData = [];
          for(var i=0;i<rowLength;i++){
              matrixData[i] = {};
              for(var j=0;j<colLength;j++){
                  matrixData[i][columns[j]] = matrix_data[i][columns[j]];
              }
          }
          //ajax
          params = {};
          params['caseId'] = caseId;     
          params['matrix'] = JSON.stringify(matrixData);
          $('#field3DMatrixWindow').window('close');
         openPostWindow(uploadUrl.maIndex, JSON.stringify(matrixData), "");
    }
},{
    text:"保存",
    iconCls:'icon-save',
    handler:function(){
        $('#threeDmatrix_data').datagrid('endEdit', editIndex);
        var columns = $('#threeDmatrix_data').datagrid('getColumnFields');
        var matrix_data = $('#threeDmatrix_data').datagrid('getRows');
        var rowLength = matrix_data.length;
        var colLength = columns.length;
        //join data
        var matrixData = "";
        for(var i=0;i<rowLength;i++){
            for(var j=1;j<colLength;j++){
                if(j!=colLength-1)
                    matrixData += matrix_data[i][columns[j]] + ";";
                else
                    matrixData += matrix_data[i][columns[j]];
            }
            if(i!=rowLength-1)
                matrixData += "#";
        }

        $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
        params = {};
        params['caseId'] = caseId;
        params['value'] = matrixData;
        params['fieldId'] = last.id;
        $.ajax({
            url:tableUrl.tMatrixZUpdate,
            type:'post',
            data:params,
            success:function(result){
                if(result.successful){
                    var show = "";
                    if(params['value'].length > 10){
                        show = params['value'].substring(0,10) + "...";
                    }else{
                        show = params['value'];
                    }
                    $('#case_data').treegrid('update',{
                        id:last.id,
                        row:{
                            data_value:show
                        }
                    });
                    $('#field3DMatrixWindow').window('close');
                }
            }
        });
    }
}];

var threeDmatrix_X_toolbar = [{
    text:"增加一行",
    iconCls:'icon-add',
    handler:function() {
        //var columns = $('#matrix_data').datagrid('getColumnFields');
        $('#threeDmatrix_data_X').datagrid('appendRow', {});
    }
}, {
    text: "删除一行",
    iconCls: 'icon-remove',
    handler: function () {
        var row = $('#threeDmatrix_data_X').datagrid('getSelected');
        var rowIndex = $('#threeDmatrix_data_X').datagrid('getRowIndex', row);
        $('#threeDmatrix_data_X').datagrid('deleteRow', rowIndex);
    }
},{
    text:"导入X",
    iconCls:'icon-undo',
    handler:function(){
        //select file
        $('#threeDmatrix_import_X').click();
    }
},{
    text:"导出X",
    iconCls:'icon-redo',
    handler:function(){
    	 $('#case_data').treegrid('endEdit',editId);
         var last = $("#case_data").treegrid('find',editId);
      $('#threeDmatrix_data_X').datagrid('endEdit', editIndex);
         var columns = $('#threeDmatrix_data_X').datagrid('getColumnFields');         
         var matrix_data = $('#threeDmatrix_data_X').datagrid('getRows');    
         var rowLength = matrix_data.length;      
         var colLength = columns.length; 
         //join data
         var matrixData = [];
         for(var i=0;i<rowLength;i++){
             matrixData[i] = {};
             for(var j=0;j<colLength;j++){
                 matrixData[i][columns[j]] = matrix_data[i][columns[j]];
             }
         }
         //ajax
         params = {};
         params['caseId'] = caseId;     
         params['matrix'] = JSON.stringify(matrixData);
         $('#X_3DMatrixWindow').window('close');
        openPostWindow(uploadUrl.maIndex, JSON.stringify(matrixData), "");
    }
},{
    text:"保存",
    iconCls:'icon-save',
    handler:function(){
        $('#threeDmatrix_data_X').datagrid('endEdit', editIndex);
        var columns = $('#threeDmatrix_data_X').datagrid('getColumnFields');
        var matrix_data = $('#threeDmatrix_data_X').datagrid('getRows');
        var rowLength = matrix_data.length;
        var colLength = columns.length;
        //join data
        var matrixData = "";
        for(var i=0;i<rowLength;i++){
            for(var j=0;j<colLength;j++){
                if(j!=colLength-1)
                    matrixData += matrix_data[i][columns[j]] + ";";
                else
                    matrixData += matrix_data[i][columns[j]];
            }
            if(i!=rowLength-1)
                matrixData += "#";
        }
        $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
        params = {};
        params['caseId'] = caseId;
        params['value'] = matrixData;
        params['fieldId'] = last.id;
        $.ajax({
            url:tableUrl.tMatrixXUpdate,
            type:'post',
            data:params,
            success:function(result){
                if(result.successful){
                    $('#X_3DMatrixWindow').window('close');
                }
            }
        });
    }
}];

var threeDmatrix_Y_toolbar = [{
    text:"增加一行",
    iconCls:'icon-add',
    handler:function() {
        //var columns = $('#matrix_data').datagrid('getColumnFields');
        $('#threeDmatrix_data_Y').datagrid('appendRow', {});
    }
}, {
    text: "删除一行",
    iconCls: 'icon-remove',
    handler: function () {
        var row = $('#threeDmatrix_data_Y').datagrid('getSelected');
        var rowIndex = $('#threeDmatrix_data_Y').datagrid('getRowIndex', row);
        $('#threeDmatrix_data_Y').datagrid('deleteRow', rowIndex);
    }
},{
    text:"导入Y",
    iconCls:'icon-undo',
    handler:function(){
        //select file
        $('#threeDmatrix_import_Y').click();
    }
},{
    text:"导出",
    iconCls:'icon-redo',
    handler:function(){
    	 $('#case_data').treegrid('endEdit',editId);
         var last = $("#case_data").treegrid('find',editId);
      $('#threeDmatrix_data_Y').datagrid('endEdit', editIndex);
         var columns = $('#threeDmatrix_data_Y').datagrid('getColumnFields');         
         var matrix_data = $('#threeDmatrix_data_Y').datagrid('getRows');    
         var rowLength = matrix_data.length;      
         var colLength = columns.length; 
         //join data
         var matrixData = [];
         for(var i=0;i<rowLength;i++){
             matrixData[i] = {};
             for(var j=0;j<colLength;j++){
                 matrixData[i][columns[j]] = matrix_data[i][columns[j]];
             }
         }
         //ajax
         params = {};
         params['caseId'] = caseId;     
         params['matrix'] = JSON.stringify(matrixData);
         $('#Y_3DMatrixWindow').window('close');
        openPostWindow(uploadUrl.maIndex, JSON.stringify(matrixData), "");
    }
},{
    text:"保存",
    iconCls:'icon-save',
    handler:function(){
        $('#threeDmatrix_data_Y').datagrid('endEdit', editIndex);
        var columns = $('#threeDmatrix_data_Y').datagrid('getColumnFields');
        var matrix_data = $('#threeDmatrix_data_Y').datagrid('getRows');
        var rowLength = matrix_data.length;
        var colLength = columns.length;
        //join data
        var matrixData = "";
        for(var i=0;i<rowLength;i++){
            for(var j=0;j<colLength;j++){
                if(j!=colLength-1)
                    matrixData += matrix_data[i][columns[j]] + ";";
                else
                    matrixData += matrix_data[i][columns[j]];
            }
            if(i!=rowLength-1)
                matrixData += "#";
        }
        $('#case_data').treegrid('endEdit',editId);
        var last = $("#case_data").treegrid('find',editId);
        params = {};
        params['caseId'] = caseId;
        params['value'] = matrixData;
        params['fieldId'] = last.id;
        $.ajax({
            url:tableUrl.tMatrixYUpdate,
            type:'post',
            data:params,
            success:function(result){
                if(result.successful){
                    $('#Y_3DMatrixWindow').window('close');
                }
            }
        });
    }
}];

$.extend($.fn.datagrid.methods, {
    editCell: function(jq,param){
        return jq.each(function(){
            var opts = $(this).datagrid('options');
            var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
            for(var i=0; i<fields.length; i++){
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field){
                    col.editor = null;
                }
            }
            $(this).datagrid('beginEdit', param.index);
            for(var i=0; i<fields.length; i++){
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    }
});
var editIndex = undefined;
function endEditing(){
    if (editIndex == undefined){return true}
    if ($('#matrix_data').datagrid('validateRow', editIndex)){
        $('#matrix_data').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}
function onClickCell(index, field){
    if (endEditing()){
        $('#matrix_data').datagrid('selectRow', index)
                .datagrid('editCell', {index:index,field:field});
        editIndex = index;
    }
}

function endEditingX(){
    if (editIndex == undefined){return true}
    if ($('#threeDmatrix_data_X').datagrid('validateRow', editIndex)){
        $('#threeDmatrix_data_X').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}
function onClickCellX(index, field){
    if (endEditingX()){
        $('#threeDmatrix_data_X').datagrid('selectRow', index)
                .datagrid('editCell', {index:index,field:field});
        editIndex = index;
    }
}
//////////////////////////////////////////////////
function endEditingY(){
    if (editIndex == undefined){return true}
    if ($('#threeDmatrix_data_Y').datagrid('validateRow', editIndex)){
        $('#threeDmatrix_data_Y').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}
function onClickCellY(index, field){
    if (endEditingY()){
        $('#threeDmatrix_data_Y').datagrid('selectRow', index)
                .datagrid('editCell', {index:index,field:field});
        editIndex = index;
    }
}
//////////////////////////////////////////////////
function endEditingZ(){
    if (editIndex == undefined){return true}
    if ($('#threeDmatrix_data').datagrid('validateRow', editIndex)){
        $('#threeDmatrix_data').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}
function onClickCellZ(index, field){
    if (endEditingZ()){
        $('#threeDmatrix_data').datagrid('selectRow', index)
                .datagrid('editCell', {index:index,field:field});
        editIndex = index;
    }
}
//二维表Excel的导入
function matrixChange(){
    var ids = [];
   ids[0] = 'matrix_import';
   $('#case_data').treegrid('endEdit',editId);
   var last = $("#case_data").treegrid('find',editId);
   var fieldId=last.id;
   params={};
   params['caseId'] = caseId; 
    var filename = $('#matrix_import').val();  
    filename = filename.substring(filename.lastIndexOf("\\")+1);  
    fileExcel = filename.substring(filename.lastIndexOf("\\")+1);  
/*     alert("aa"+fileExcel.substring(fileExcel.lastIndexOf(".")+1));
    if(!fileExcel.substring(fileExcel.lastIndexOf(".")+1).equest("xls")|| ! fileExcel.substring(fileExcel.lastIndexOf(".")+1).equest("xlsx")){
	   alert("上传失败！必须是Excel文件！");
	  // $('#fieldMatrixWindow').window('close');
   }  */     
  // alert(caseId+"\t"+fieldId);
    $.ajaxFileUpload({
        url:uploadUrl.uploadMatrix,
        secureuri:false,
        fileElementId:ids,          
       // dataType:'json',
        success:function(data,status){        
            if(data){
                //if file uploaded is json 
                $.ajax({
                    url:uploadUrl.readyMatrixExcel,
                    data:'filename='+filename+'&caseId='+caseId+'&fieldId='+fieldId,
                    type:'get',
                    success:function(result){                  
                        if(result.successful){
                        	alert("上传成功！");
                     /*    	 var matrixJson = result.message;
                             alert(matrixJson);
                             matrixJson = eval("("+matrixJson+")");   */
                            $('#fieldMatrixWindow').window('close');
                            <%--  $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+caseId); --%>
                            $('#matrix_data').datagrid('reload');    
                            /* $('#matrix_data').datagrid({
                              //  data:matrixJson
                            });   */                         
                        }                         
                    },
                error:function(result){
                	alert("上传失败");
                }
                });
            }
            else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){
        }
    });
}
//二维表json方式导入
function matrixChange11222(){
    var ids = [];
   ids[0] = 'matrix_import';
   $('#case_data').treegrid('endEdit',editId);
   var last = $("#case_data").treegrid('find',editId);
   var fieldId=last.id;
   params={};
   params['caseId'] = caseId; 
   alert(fieldId);
   alert(caseId);
    var filename = $('#matrix_import').val();  
    alert("1"+filename);
    filename = filename.substring(filename.lastIndexOf("\\")+1);
    alert("2"+filename);
    $.ajaxFileUpload({
        url:uploadUrl.uploadMatrix,
        secureuri:false,
        fileElementId:ids,          
        dataType:'json',
        success:function(data,status){
            if(data){
                //if file uploaded is json 
                $.ajax({
                    url:uploadUrl.readMatrixJson,
                    data:'filename='+filename,
                    type:'get',
                    success:function(result){
                        if(result.successful){
                            var matrixJson = result.message;
                            alert(matrixJson);
                            matrixJson = eval("("+matrixJson+")");                       
                            $('#matrix_data').datagrid({
                                data:matrixJson
                            });                           
                        } 
                    }
                });
            }
            else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){

        }
    });
}

//三维表导入
function threeDmatrixChange(){
	  var ids = [];
	   ids[0] = 'threeDmatrix_import';
	   $('#case_data').treegrid('endEdit',editId);
	//获取属性项的id
	var last = $("#case_data").treegrid('find',editId);
	var fieldId=last.id;
    var filename = $('#threeDmatrix_import').val();  
    filename = filename.substring(filename.lastIndexOf("\\")+1);   
    $.ajaxFileUpload({
        url:uploadUrl.uploadThreeMatrix,
        secureuri:false,
        fileElementId:ids,          
       // dataType:'json',
        success:function(data,status){        
            if(data){
                //if file uploaded is json 
                $.ajax({
                    url:uploadUrl.readyThreeMatrixExcel,
                    data:'filename='+filename+'&caseId='+caseId+'&fieldId='+fieldId,
                    type:'get',
                    success:function(result){                  
                        if(result.successful){
                        	alert("上传成功！");
                            $('#field3DMatrixWindow').window('close'); 
                            <%--  $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+caseId); --%>
                            $('#threeDmatrix_import').datagrid('reload');    
                            /* $('#matrix_data').datagrid({
                              //  data:matrixJson
                            });   */                         
                        }                         
                    },
                error:function(result){
                	alert("上传失败");
                }
                });
            }
            else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){
        }
    }); 
}
function threeDmatrixChangeX(){
	var ids = [];
	   ids[0] = 'threeDmatrix_import_X';
	   $('#case_data').treegrid('endEdit',editId);
	//获取属性项的id
	var last = $("#case_data").treegrid('find',editId);
	var fieldId=last.id;
 var filename = $('#threeDmatrix_import_X').val();  
 filename = filename.substring(filename.lastIndexOf("\\")+1);   
 $.ajaxFileUpload({
     url:uploadUrl.uploadThreeMatrix,
     secureuri:false,
     fileElementId:ids,          
    // dataType:'json',
     success:function(data,status){        
         if(data){
             //if file uploaded is json 
             $.ajax({
                 url:uploadUrl.readyThreeMatrixExcel,
                 data:'filename='+filename+'&caseId='+caseId+'&fieldId='+fieldId,
                 type:'get',
                 success:function(result){                  
                     if(result.successful){
                     	alert("上传成功！");
                         $('#X_3DMatrixWindow').window('close'); 
                         <%--  $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+caseId); --%>
                         $('#threeDmatrix_import_X').datagrid('reload');    
                         /* $('#matrix_data').datagrid({
                           //  data:matrixJson
                         });   */                         
                     }                         
                 },
             error:function(result){
             	alert("上传失败");
             }
             });
         }
         else{
             $.messager.alert("提示",data.result,"error");
         }
     },
     error:function(data,status,e){
     }
 }); 
}
function threeDmatrixChangeY(){
	var ids = [];
	   ids[0] = 'threeDmatrix_import_Y';
	   $('#case_data').treegrid('endEdit',editId);
	//获取属性项的id
	var last = $("#case_data").treegrid('find',editId);
	var fieldId=last.id;
 var filename = $('#threeDmatrix_import_Y').val();  
 filename = filename.substring(filename.lastIndexOf("\\")+1);   
 $.ajaxFileUpload({
     url:uploadUrl.uploadThreeMatrix,
     secureuri:false,
     fileElementId:ids,          
    // dataType:'json',
     success:function(data,status){        
         if(data){
             //if file uploaded is json 
             $.ajax({
                 url:uploadUrl.readyThreeMatrixExcel,
                 data:'filename='+filename+'&caseId='+caseId+'&fieldId='+fieldId,
                 type:'get',
                 success:function(result){                  
                     if(result.successful){
                     	alert("上传成功！");
                         $('#Y_3DMatrixWindow').window('close'); 
                         <%--  $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+caseId); --%>
                         $('#threeDmatrix_import_Y').datagrid('reload');    
                         /* $('#matrix_data').datagrid({
                           //  data:matrixJson
                         });   */                         
                     }                         
                 },
             error:function(result){
             	alert("上传失败");
             }
             });
         }
         else{
             $.messager.alert("提示",data.result,"error");
         }
     },
     error:function(data,status,e){
     }
 }); 
}
function threeDmatrixChangejson(){
    var ids = [];
    ids[0] = 'threeDmatrix_import';
    var filename = $('#threeDmatrix_import').val();
    filename = filename.substring(filename.lastIndexOf("\\")+1);
    $.ajaxFileUpload({
        url:uploadUrl.uploadMatrix,
        secureuri:false,
        fileElementId:ids,
        dataType:'json',
        success:function(data,status){
            if(data){
                //if file uploaded is json
                $.ajax({
                    url:uploadUrl.readMatrixJson,
                    data:'filename='+filename,
                    type:'get',
                    success:function(result){
                        if(result.successful){
                            var matrixJson = result.message;
                            alert(matrixJson);
                            matrixJson = eval("("+matrixJson+")");
                            $('#threeDmatrix_data').datagrid({
                                data:matrixJson
                            });

                        }
                    }
                });
            }
            else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){

        }
    });
}
function openPostWindow(url, data, name) {
    var tempForm = document.createElement("form");
    tempForm.id = "tempForm1";
    tempForm.method = "post";
    tempForm.action = url;
    tempForm.target = name;
    var hideInput = document.createElement("input");
    hideInput.type = "hidden";
    hideInput.name = "content"
    hideInput.value = data;
    tempForm.appendChild(hideInput);
    //firefox,chrome, if IE attachEvent
    tempForm.addEventListener("onsubmit", function () {
       // openWindow(name);
    });
    document.body.appendChild(tempForm);
    //firefox,chrome, if IE fireEvent
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("onsubmit", false, false);
    tempForm.dispatchEvent(evt);
    tempForm.submit();
    document.body.removeChild(tempForm);
}
/* function openWindow(name) {
    window.open(name);
} */
</script>
</body>
</html>